package com.airlines.crewassignment.service.dict.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.config.cglib.ReflectUtil;
import com.airlines.base.entity.dict.Select2;
import com.airlines.common.TraceUtil;
import com.airlines.crewassignment.constant.C3092Constant;
import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.dao.select2.Select2Mapper;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.service.crew.impl.CrewRankConvertServiceImpl;
import com.airlines.crewassignment.service.dict.Select2Service;

/**
 * @author xxxx
 * @date 2018年6月7日
 * @description
 */

@Service
public class Select2ServiceImpl implements Select2Service {
	@Autowired
	private Select2Mapper select2Mapper;
	static Log log = LogFactory.getLog(CrewRankConvertServiceImpl.class);

	public List<Select2> getSelect2FromC3092(String parentCode, String dutyCode) {
		return select2Mapper.getSelect2FromC3092(parentCode, dutyCode);
	}

	@Override
	public List<Select2> getPositionTsFlagF() {
		return select2Mapper.getSelect2FromC3092(C3092Constant.PARENT_CODE_POSITION_TS_FLAG_F, CrewConstant.DUTY_F);
	}

	@Override
	public List<Select2> getPositionTsFlagC() {
		return select2Mapper.getSelect2FromC3092(C3092Constant.PARENT_CODE_POSITION_TS_FLAG_C, CrewConstant.DUTY_C);

	}

	@Override
	public List<Select2> getPositionCheckThingF() {
		return select2Mapper.getSelect2FromC3092(C3092Constant.PARENT_CODE_POSITION_CHECK_THING, CrewConstant.DUTY_F);

	}

	@Override
	public List<Select2> getCrewNumberType(String dutyCode) {
		return select2Mapper.getCrewNumberType(dutyCode);
	}

	@Override
	public List<Select2> getAirportCityCnName() {
		// TODO Auto-generated method stub
		return select2Mapper.getAirportCityCnName();
	}

	@Override
	public List<Select2> getAirportCityAbbr() {
		// TODO Auto-generated method stub
		return select2Mapper.getAirportCityAbbr();
	}

	@Override
	public String getText(List<Select2> list, String id) {
		int i = 0;
		String text = null;
		for (Select2 s : list) {
			if (StringUtils.equals(id, s.getId())) {
				text = s.getText();
				i++;
			}
		}

		if (i == 1) {
			return text;
		} else {
			return null;
		}

	}

	@Override
	public <T> List<T> convert(List<T> list, String idColumnName, String valueColumnName, List<Select2> selectList) {
		for (T t : list) {
			String id = null;
			try {
				id = BeanUtils.getProperty(t, idColumnName);
				if (StringUtils.isEmpty(id)) {
					continue;
				}
				String value = this.getText(selectList, id);
				BeanUtils.setProperty(t, valueColumnName, value);
			} catch (Exception e) {
				log.error(e);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> convert(List<T> list, String idColumnName, List<Select2> selectList) {
		List<T> objectList = new ArrayList<>();
		for (T t : list) {
			String id = null;
			try {
				id = BeanUtils.getProperty(t, idColumnName);
				if (StringUtils.isEmpty(id)) {
					continue;
				}
				String value = this.getText(selectList, id);
				Map<String, Object> addProperties = new HashMap<>();
				String idColumnNameSelf = "name_" + idColumnName;
				addProperties.put(idColumnNameSelf, value);
				T o = (T) ReflectUtil.getTarget(t, addProperties);
				objectList.add(o);
			} catch (Exception e) {
				log.error(e);
			}
		}
		return objectList;
	}

	@Override
	public <T> List<T> convert(List<T> list, Map<String, List<Select2>> selectMap) {
		// 循环每个属性
		for (Map.Entry<String, List<Select2>> entry : selectMap.entrySet()) {
			String idColumnName = entry.getKey();
			List<Select2> selectList = entry.getValue();
			list = this.convert(list, idColumnName, selectList);
		}

		return list;
	}

	@Override
	public List<Select2> getCrewName() {
		return select2Mapper.getCrewName();
	}

	@Override
	public List<Select2> getTechNoZ(String techType) {
		return select2Mapper.getTechNoZ(techType);
	}

	@Override
	public List<Select2> getYandN() {
		List<Select2> list = new ArrayList<>();
		Select2 select1 = new Select2();
		select1.setId("Y");
		select1.setText("能");
		Select2 select2 = new Select2();
		select2.setId("N");
		select2.setText("否");
		list.add(select1);
		list.add(select2);
		return list;
	}

	@Override
	public List<Select2> getFlightNoList(Date flightDateStart, Date flightDateEnd) {
		List<FlightInfo> list = select2Mapper.getFlightNoList(flightDateStart, flightDateEnd);
		List<Select2> airportList = this.getAirportCityCnName();
		List<Select2> select2List = new ArrayList<>();
		String departureAirport = "departureAirport";
		String arrivalAirport = "arrivalAirport";
		for (FlightInfo f : list) {
			Select2 select2 = new Select2();
			// select2.setId(f.getFlightNo());

			String departureAirportName = this.getText(airportList, f.getDepartureAirport());
			String arrivalAirportName = this.getText(airportList, f.getArrivalAirport());

			String text = f.getFlightNo() + "(" + departureAirportName + "-" + arrivalAirportName + ")";
			select2.setText(text);
			select2.setId(text);

			Map<String, String> map = new HashMap<>();
			map.put("flightNo", f.getFlightNo());
			map.put(departureAirport, f.getDepartureAirport());
			map.put("departureAirportName", departureAirportName);
			map.put(arrivalAirport, f.getArrivalAirport());
			map.put("arrivalAirportName", arrivalAirportName);
			select2.setMap(map);

			select2List.add(select2);
		}

		return select2List;

	}

	@Override
	public List<Select2> getFlightPosition(String rankFlag) {
		return select2Mapper.getFlightPosition(rankFlag);
	}

	@Override
	public List<Select2> getSelect2(String dictionaryCode) {
		return select2Mapper.getSelect2(dictionaryCode);
	}

}
